package sample.ws.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sample.ws.entity.ServiceAccessRuleEntity;
import sample.ws.entity.ServiceEntity;
import sample.ws.entity.ServiceIPRuleEntity;
import sample.ws.model.RuleModel;
import sample.ws.service.IService;

import java.util.List;

/**
 * WsController..
 *
 * @author Lizhong
 * @date 2019/7/16
 */
@RestController
@RequestMapping("/ws")
public class WsController {
    private final IService iService;

    public WsController(IService iService) {
        this.iService = iService;
    }

    @GetMapping("/{id}/open")
    public ResponseEntity.BodyBuilder openService(
            @PathVariable Long id
    ) {
        iService.openServiceById(id);
        return ResponseEntity.ok();
    }

    @GetMapping("/{id}/close")
    public ResponseEntity.BodyBuilder closeService(
            @PathVariable Long id
    ) {
        iService.closeServiceById(id);
        return ResponseEntity.ok();
    }

    @GetMapping
    public ResponseEntity<List<ServiceEntity>> getServices(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size
    ) {
        List<ServiceEntity> serviceList = iService.findServiceList(category,page,size);
        return ResponseEntity.ok(serviceList);
    }

    @PostMapping
    public ResponseEntity.BodyBuilder putService(
            @RequestBody ServiceEntity serviceEntity
    ) {
       boolean f = iService.add(serviceEntity);
        return ResponseEntity.ok();
    }


    /**访问控制层次*/
    @PutMapping("/black-list")
    public ResponseEntity<String> putIpBlackList(
            @RequestBody ServiceIPRuleEntity ipRuleEntity
    ) {
        boolean b = iService.putIpBackListById(ipRuleEntity);
        if (b) return ResponseEntity.ok("添加成功");
        return ResponseEntity.ok("添加失败");
    }

    @GetMapping("/black-list")
    public ResponseEntity<List<ServiceIPRuleEntity>> getIpBlackList(
    ) {
        List<ServiceIPRuleEntity> list0 = iService.getIpBackList();
        return ResponseEntity.ok(list0);
    }

    @PutMapping("/access-rule")
    public ResponseEntity<String> putAccessRule(
            @RequestBody RuleModel rule
    ) {
        boolean b = iService.putAccessRule(rule.getServiceId(), rule);
        if (b) return ResponseEntity.ok("添加成功");
        return ResponseEntity.ok("添加失败");
    }

    @GetMapping("/access-rule")
    public ResponseEntity<List<ServiceAccessRuleEntity>> getAccessRule(
    ) {
        List<ServiceAccessRuleEntity> b = iService.getAccessRules();
        return ResponseEntity.ok(b);
    }
}
